﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Communication;
using Core;
using Core.Performance;

namespace Service.Agent
{
    public class MainService
    {
        #region internalVariables

        #region private
        private bool isCommandLine;
        private Counters counters;
        #endregion

        #region public
        public static int Count = 0;
        public static List<Entity> EntityList = new List<Entity>();
        public static List<EntityProperty> EntityPropertyList = new List<EntityProperty>();
        public static List<EntityType> EntityTypeList = new List<EntityType>();
        public static List<EntityTypeProperty> EntityTypePropertyList = new List<EntityTypeProperty>();
        public static List<AlertRule> AlertRuleList = new List<AlertRule>();
        #endregion

        #endregion

        public MainService()
        {
        }
        
        public bool IsCommandLine
        {
            get { return isCommandLine; }
            set { 
                isCommandLine = value;
                Logging.IsCommandLine = isCommandLine;
            }
        }

        public void Checks(object state)
        {
            if (Count == 60)
                Count = 0;      // resetting the run count, although it might be wiser to use DateTime.Now to calculate where we are timewise...
            Logging.LogInformation("Performing check...");
            Logging.LogInformation("We are at run " + Count.ToString());

            if (Helper.Fqdn.Trim() == string.Empty)
            {
                Logging.LogInformation("Unable to resolve machinename");
                Logging.LogInformation("Exiting this run...");
                return;
            }

            Entity e = new Entity();
            e.EntityTypeId = EntityTypeList.Where(etp => etp.Uid == "71752005-F6F7-4F37-84CF-07C1869562FE").FirstOrDefault().Id;
            e.Name = Helper.Fqdn;
            e.IsDeleted = false;
            e.Id = ForwarderCommunication.AddEntity(e);

            foreach (SqlInstance instance in Discover.EnumerateSqlInstances())
            {
                Logging.LogInformation("Checking instance: " + instance.Name);
                SqlCheck.PerformCheck(instance, e, e);
            }
            Logging.LogInformation("Ending check...");
            Count++;
        }

        public void Init()
        {            
            if (EntityTypeList.Count == 0)
            {
                Logging.LogInformation("Caching internal tables");
                
                //When caching is implemented, the part what hapens when null should only be done when there is no cache
                EntityTypeList = ForwarderCommunication.GetEntityTypeList() ?? new List<EntityType>();
                EntityTypePropertyList = ForwarderCommunication.GetEntityTypePropertyList() ?? new List<EntityTypeProperty>();
                AlertRuleList = ForwarderCommunication.GetAlertRuleList() ?? new List<AlertRule>();
                //TODO: fill other tables. need SP's first.
            }

            Logging.LogInformation("Initializing Performance counters");
            counters = new Counters();
            //counters.DimDateList = ForwarderCommunication.GetDateList() ?? new List<dimDate>();
            //counters.DimPerfMonCounterList = ForwarderCommunication.GetPerfMonCounterList() ?? new List<dimPerfMonCounter>();
            //counters.DimPerfMonList = ForwarderCommunication.GetPerfMonList() ?? new List<dimPerfMon>();
            counters.DimDeviationPercentList = ForwarderCommunication.GetDeviationPercentList() ?? new List<DeviationPercent>();
        }
    }
}

